#include "logger.h"
#include <iostream>

void exit_test (int status) {
  cout << "Custom exit function. Note that no logging is possible here." <<
    endl;
   if (status == EXIT_SUCCESS) {
    cerr << "Quitted after a fatal error." << endl;
  }
  else {
    cout << "Quitted normally." << endl;
  }
  exit(status);
}

void test () {
  LOG ("Test begins");
  int i = 1982;
  LOG_VAR (i);
  double pi_approx = 3.14159;
  LOG_DEBUG("Pi debug");
  LOGN ("Pi approximatively equals : ");
  LOG (pi_approx);

  LOG_WARNING("Pi was not approximated accurately");
  LOG_ERROR("Cannot compute a precise value of the circle area");

  LOGN_INFO("Info starts here, ");
  LOG_INFO("and finishes here.");

  LOGN_WARNING("Warning starts here, ");
  LOG_WARNING("and finishes here.");

  LOGN_ERROR("Error starts here, ");
  LOG_ERROR("and finishes here.");
}

int main (int argc, char** argv) {
  const char* filename = NULL;
  // true to execute a test finishing with a fatal error.
  bool fatal = false;
  
  for (int k = 1; k < argc; ++k) {
    if (!strcmp(argv[k], "--fatal")) {
      fatal = true;
    }
    else {
      filename = argv[k];
    }
  }

  /**
    * Normal test
    */
  logger_init (filename, &exit_test);
  test();
  if (fatal) {
    LOG_FATAL("Fatal error.");
  }
  else {
    LOG("Normal quit.");
    logger_quit();
  }
  /**
    * Never reached.
    */
  cout << "Should never appear on the console." << endl;
  return (EXIT_SUCCESS);
}

